//
//  BSystemAlert.m
//  iphoneLive⁩
//
//  Created by  on 2019/10/14.
//  Copyright © 2019 _ios. All rights reserved.
//

#import "BSystemAlert.h"

@implementation BSystemAlert


- (void)showAlertWithTitle:(NSString *)title
                   message:(NSString *)message
                  userData:(id)userData
               cancelTitle:(NSString *)cancelTitle
                   actions:(NSArray<NSString *> *)actions
               actionBlock:(void(^)(id userData,NSString *actionTitle))actionBlock{
    
    [self showAlertWithTitle:title
                     message:message
                    userData:userData
                 cancelTitle:cancelTitle
                 cancelColor:nil
                     actions:actions
                 actionBlock:actionBlock];
}

- (void)showAlertWithTitle:(NSString *)title
                   message:(NSString *)message
                  userData:(id)userData
               cancelTitle:(NSString *)cancelTitle
               cancelColor:(UIColor *)cancelColor
                   actions:(NSArray<NSString *> *)actions
               actionBlock:(void(^)(id userData,NSString *actionTitle))actionBlock{
    [self showAlertWithTitle:title
                     message:message
                    userData:userData
                 cancelTitle:cancelTitle
                 cancelColor:cancelColor
                     actions:actions
                    actionCs:@[]
                 actionBlock:actionBlock];
}

- (void)showAlertWithTitle:(NSString *)title
                   message:(NSString *)message
                  userData:(id)userData
               cancelTitle:(NSString *)cancelTitle
                   actions:(NSArray<NSString *> *)actions
                  actionCs:(NSArray<UIColor *> *)actionCs
               actionBlock:(void(^)(id userData,NSString *actionTitle))actionBlock{
    [self showAlertWithTitle:title
                     message:message
                    userData:userData
                 cancelTitle:cancelTitle
                 cancelColor:nil
                     actions:actions
                    actionCs:@[]
                 actionBlock:actionBlock];
}

- (void)showAlertWithTitle:(NSString *)title
                   message:(NSString *)message
                  userData:(id)userData
               cancelTitle:(NSString *)cancelTitle
               cancelColor:(UIColor *)cancelColor
                   actions:(NSArray<NSString *> *)actions
                  actionCs:(NSArray<UIColor *> *)actionCs actionBlock:(void(^)(id userData,NSString *actionTitle))actionBlock{
    NSString *tStr = NSLocalizedString(title, title);
    if(title.length == 0){
        tStr = nil;
    }
    NSString *mStr = NSLocalizedString(message, message);
    if(message.length == 0){
        mStr = nil;
    }
    
    UIAlertController *controller = [UIAlertController alertControllerWithTitle:tStr message:mStr preferredStyle:UIAlertControllerStyleAlert];
    
    UIView *v = nil;
    if([[UIDevice currentDevice].model isEqualToString:@"iPad"]){
        UIPopoverPresentationController *popover = controller.popoverPresentationController;
        v = [[UIView alloc] init];
        CGFloat w = [UIScreen mainScreen].bounds.size.width;
        CGFloat h = [UIScreen mainScreen].bounds.size.height;
        
        CGFloat vW = 1;
        
        v.frame = CGRectMake((w - vW) * 0.5, (h - vW) * 0.5f, vW, vW);
        v.userInteractionEnabled = NO;
        [[self visibleViewController].view addSubview:v];
        popover.sourceView = v;
        popover.sourceRect = v.bounds;
        popover.permittedArrowDirections = UIPopoverArrowDirectionUnknown;
    }
    
    __block typeof(actionBlock) tmpActionBlock = actionBlock;
    __block typeof(userData) tmpUserData = userData;
    NSInteger index = 0;
    for (NSString *actionTitle in actions) {
        UIAlertAction *tAction = [UIAlertAction actionWithTitle:NSLocalizedString(actionTitle, actionTitle) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            if(nil != v){
                [v removeFromSuperview];
            }
            if(tmpActionBlock){
                tmpActionBlock(tmpUserData,action.title);
            }
        }];
        if(actionCs.count > index){
            
            [self changeAction:tAction color:actionCs[index]];
        }
        index++;
        [controller addAction:tAction];
    }
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(cancelTitle, cancelTitle) style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        if(nil != v){
            [v removeFromSuperview];
        }
        if(tmpActionBlock){
            tmpActionBlock(tmpUserData,action.title);
        }
    }];
    
    [self changeAction:cancelAction color:cancelColor];
    [controller addAction:cancelAction];
    [[self visibleViewController] presentViewController:controller animated:YES completion:^{
        
    }];
}

- (void)showActionSheetWithTitle:(NSString *)title message:(NSString *)message userData:(id)userData cancelTitle:(NSString *)cancelTitle actions:(NSArray<NSString *> *)actions actionBlock:(void(^)(id userData,NSString *actionTitle))actionBlock{
    NSString *tStr = NSLocalizedString(title, title);
    if(title.length == 0){
        tStr = nil;
    }
    NSString *mStr = NSLocalizedString(message, message);
    if(message.length == 0){
        mStr = nil;
    }
    UIAlertController *controller = [UIAlertController alertControllerWithTitle:tStr message:mStr preferredStyle:UIAlertControllerStyleActionSheet];
    UIView *v = nil;
    if([[UIDevice currentDevice].model isEqualToString:@"iPad"]){
        UIPopoverPresentationController *popover = controller.popoverPresentationController;
        v = [[UIView alloc] init];
        CGFloat w = [UIScreen mainScreen].bounds.size.width;
        CGFloat h = [UIScreen mainScreen].bounds.size.height;
        
        CGFloat vW = 1;
        
        v.frame = CGRectMake((w - vW) * 0.5, (h - vW) * 0.5f, vW, vW);
        v.userInteractionEnabled = NO;
        [[self visibleViewController].view addSubview:v];
        popover.sourceView = v;
        popover.sourceRect = v.bounds;
        popover.permittedArrowDirections = UIPopoverArrowDirectionDown;
    }
    
    __block typeof(actionBlock) tmpActionBlock = actionBlock;
    __block typeof(userData) tmpUserData = userData;
    for (NSString *actionTitle in actions) {
        UIAlertAction *tAction = [UIAlertAction actionWithTitle:NSLocalizedString(actionTitle, actionTitle) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            if(nil != v){
                [v removeFromSuperview];
            }
            if(tmpActionBlock){
                tmpActionBlock(tmpUserData,action.title);
            }
        }];
        [controller addAction:tAction];
    }
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(cancelTitle, cancelTitle) style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        if(nil != v){
            [v removeFromSuperview];
        }
    }];
    [controller addAction:cancelAction];
    
    [[self visibleViewController] presentViewController:controller animated:YES completion:^{
        
    }];
}


- (UIViewController *)visibleViewController{
    return [self getVisibleViewControllerFromVC:ROOT_VC()];
}

- (UIViewController *)getVisibleViewControllerFromVC:(UIViewController *)vc{
    if(vc && [vc isKindOfClass:[UINavigationController class]]){
        return [self getVisibleViewControllerFromVC:[(UINavigationController *)vc visibleViewController]];
    }
    else if (vc && [vc isKindOfClass:[UITabBarController class]]){
        return [self getVisibleViewControllerFromVC:[(UITabBarController *)vc selectedViewController]];
    }
    else{
        if(vc && vc.presentedViewController){
            return [self getVisibleViewControllerFromVC:vc.presentedViewController];
        }
        else
        {
            return vc;
        }
    }
}


+ (instancetype)shared{
    static BSystemAlert *alert = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        alert = [[BSystemAlert alloc] init];
    });
    return alert;
}
+ (instancetype)shared_chang:(BOOL)cheng{
    static BSystemAlert *alert = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        alert = [[BSystemAlert alloc] init];
//        [alert change];
    });
    return alert;
}
- (void)changeAction:(UIAlertAction *)action color:(UIColor *)color{
    //修改message
//    NSMutableAttributedString *alertControllerMessageStr = [[NSMutableAttributedString alloc] initWithString:controller.message];
//    [alertControllerMessageStr addAttribute:NSForegroundColorAttributeName value:COLOR_ALPH(35, 35, 35, 0.6) range:NSMakeRange(0, controller.message.length)];
//    [alertControllerMessageStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(0, controller.message.length)];
//    [controller setValue:alertControllerMessageStr forKey:@"attributedMessage"];
    
    //修改按钮
    if(color){
        [action setValue:MainColor forKey:@"_titleTextColor"];
    }
    
}
@end
